package com.happok.onlineclass.usermanagement.user.controller;

import com.happok.onlineclass.usermanagement.authentication.dto.LoginResultDTO;
import com.happok.onlineclass.usermanagement.common.dto.IdDTO;
import com.happok.onlineclass.usermanagement.user.dto.*;
import com.happok.onlineclass.usermanagement.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.net.URISyntaxException;

/**
 * 个人操作用户
 * 个人查询修改用户信息接口
 *
 * @author : chenac
 * @module 在线课堂/用户管理          yapi 项目组/子项目
 * @date : 2021/6/28 17:20
 */
@RestController
@Api(tags = "个人操作用户")
@RequestMapping(value = "/user-management/personal/users")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 修改个人用户
     * 个人中心修改自己的信息 <br>
     * 20000，修改个人用户信息成功 <br>
     * 40000，参数异常 <br>
     * 50000，服务端异常
     *
     * @param user          头信息
     * @param updateUserDTO
     * @author: chenac
     * @date: 14:36
     * @execption:
     * @return: void
     */
    @PutMapping(value = "/v1")
    @ApiOperation(value = "修改个人用户")
    @ApiResponses({
            @ApiResponse(code = 20000, message = "修改个人用户信息成功"),
            @ApiResponse(code = 40000, message = "参数异常"),
            @ApiResponse(code = 50000, message = "服务端异常")
    })
    public void updatePersonalUser(@RequestHeader String user,
                                   @RequestBody @Valid UpdateUserDTO updateUserDTO) throws Exception {
        userService.updatePersonalUser(user, updateUserDTO);
    }

    /**
     * 查询个人用户详情
     * 个人中心查询自己的详细信息 <br>
     * 20000，修改个人用户信息成功<br>
     * 50000，服务端异常
     *
     * @param user 头信息
     * @author: chenac
     * @date: 14:37
     * @execption:
     * @return: com.happok.onlineclass.usermanagement.user.dto.UserDetailsDTO
     */
    @GetMapping(value = "/v1")
    @ApiOperation(value = "查询个人用户详情")
    @ApiResponses({
            @ApiResponse(code = 20000, message = "修改个人用户信息成功"),
            @ApiResponse(code = 50000, message = "服务端异常")
    })
    public UserDetailDTO getPersonalUserDetail(@RequestHeader String user) throws URISyntaxException {
        return userService.getPersonalUserDetail(user);
    }

    /**
     * 用户注册
     * 个人用户使用此接口注册到平台<br>
     * 20000,用户注册成功<br>
     * 40000,参数异常<br>
     * 40903,电话号码重复<br>
     * 40904,邮箱重复<br>
     * 41001,用户注册功能未开启<br>
     * 41201,验证码不正确<br>
     * 41202,验证码失效<br>
     * 50000,服务端异常
     *
     * @param registerUserDTO
     * @author: chenac
     * @date: 11:35
     * @execption:
     * @return: com.happok.onlineclass.usermanagement.authentication.dto.LoginResultDTO
     */
    @PostMapping(value = "/register/v1")
    @ApiOperation(value = "用户注册")
    @ApiResponses({
            @ApiResponse(code = 20000, message = "用户注册成功"),
            @ApiResponse(code = 40000, message = "参数异常"),
            @ApiResponse(code = 40903, message = "电话号码重复"),
            @ApiResponse(code = 40904, message = "邮箱重复"),
            @ApiResponse(code = 41001, message = "用户注册功能设置异常"),
            @ApiResponse(code = 41201, message = "验证码不正确"),
            @ApiResponse(code = 41202, message = "验证码失效"),
            @ApiResponse(code = 50000, message = "服务端异常")
    })
    public LoginResultDTO registerUser(@RequestBody @Valid RegisterUserDTO registerUserDTO,
                                       HttpServletRequest req) throws URISyntaxException {
        return userService.registerUser(registerUserDTO, req);
    }

    /**
     * 忘记密码
     * 个人重置密码<br>
     * 20000,忘记密码成功<br>
     * 40000,参数异常<br>
     * 40104,账号被禁用<br>
     * 40400,账号不存在<br>
     * 41001,忘记密码功能设置异常<br>
     * 41201,验证码不正确<br>
     * 41202,验证码失效<br>
     * 50000,服务端异常
     * <p>
     * 接口描述
     *
     * @param personalResetPwdDTO
     * @author: chenac
     * @date: 11:50
     * @execption:
     * @return: void
     */
    @PutMapping(value = "/reset/password/v1")
    @ApiOperation(value = "忘记密码-个人重置密码")
    @ApiResponses({
            @ApiResponse(code = 20000, message = "忘记密码成功"),
            @ApiResponse(code = 40000, message = "参数异常"),
            @ApiResponse(code = 40104, message = "账户被禁用"),
            @ApiResponse(code = 40903, message = "电话号码重复"),
            @ApiResponse(code = 40904, message = "邮箱重复"),
            @ApiResponse(code = 41002, message = "忘记密码-个人重置密码功能设置异常"),
            @ApiResponse(code = 41201, message = "验证码不正确"),
            @ApiResponse(code = 41202, message = "验证码失效"),
            @ApiResponse(code = 50000, message = "服务端异常")
    })
    public IdDTO personalResetPwd(@RequestBody @Valid PersonalResetPwdDTO personalResetPwdDTO) {
        return userService.personalResetPwd(personalResetPwdDTO);
    }

    /**
     * 修改手机号或邮箱
     * 20000,修改手机号或邮箱成功<br>
     * 40000,参数异常<br>
     * 40402,账号不存在<br>
     * 40904,账号已被使用<br>
     * 41001,修改手机号或邮箱功能设置异常<br>
     * 41201,验证码不正确<br>
     * 41202,验证码失效<br>
     * 41205,手机号或邮箱不能与之前的相同<br>
     * 50000,服务端异常
     *
     * @param user                 头信息
     * @param updateUserAccountDTO
     * @author: chenac
     * @date: 12:02
     * @execption:
     * @return: void
     */
    @PutMapping(value = "/account/v1")
    @ApiOperation(value = "修改手机号或邮箱")
    @ApiResponses({
            @ApiResponse(code = 20000, message = "修改手机号或邮箱成功"),
            @ApiResponse(code = 40000, message = "参数异常"),
            @ApiResponse(code = 40402, message = "账号不存在"),
            @ApiResponse(code = 40904, message = "账号已被使用"),
            @ApiResponse(code = 41001, message = "修改手机号或邮箱功能设置异常"),
            @ApiResponse(code = 41201, message = "验证码不正确"),
            @ApiResponse(code = 41202, message = "验证码失效"),
            @ApiResponse(code = 41205, message = "手机号或邮箱不能与之前的相同"),
            @ApiResponse(code = 50000, message = "服务端异常")
    })
    public void updateUserAccount(@RequestHeader String user,
                                  @RequestBody @Valid UpdateUserAccountDTO updateUserAccountDTO) {
        userService.updateUserAccount(user, updateUserAccountDTO);
    }

    /**
     * 修改密码
     * 20000,修改密码成功<br>
     * 40000,参数异常<br>
     * 41203,旧密码不正确<br>
     * 41204,不满足修改密码条件<br>
     * 50000,服务端异常
     *
     * @param user
     * @param updateUserPwd
     * @author: chenac
     * @date:  18:19
     * @execption:
     * @return: void
     */
    @PutMapping(value = "/password/v1")
    @ApiOperation(value = "修改密码")
    @ApiResponses({
            @ApiResponse(code = 20000, message = "修改密码成功"),
            @ApiResponse(code = 40000, message = "参数异常"),
            @ApiResponse(code = 40002, message = "新密码不能与旧密码相同"),
            @ApiResponse(code = 41203, message = "旧密码不正确"),
            @ApiResponse(code = 41204, message = "不满足修改密码条件"),
            @ApiResponse(code = 50000, message = "服务端异常")
    })
    public void updateUserPwd(@RequestHeader String user,
                              @RequestBody @Valid UpdateUserPwd updateUserPwd) {
        userService.updateUserPwd(user, updateUserPwd);
    }
}